package com.usian.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.usian.common.dtos.PageResult;
import com.usian.common.dtos.ResponseResult;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.util.BeanHelper;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.dtos.WmNewsResultDTO;
import com.usian.wemedia.entity.WmNews;
import com.usian.wemedia.entity.WmUser;
import com.usian.wemedia.service.IWmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.wemedia.service.IWmNewsService;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 自媒体图文内容信息表 前端控制器
 * </p>
 *
 * @author usian
 * @since 2022-10-04
 */
@RestController
@RequestMapping("/api/v1/news")
@Api(tags = "自媒体图文内容信息表接口")
@CrossOrigin
public class WmNewsController{

    @Autowired
    private IWmNewsService wmNewsService;

    @Autowired
    private IWmUserService wmUserService;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/page")
    public PageResult<WmNewsDto> findPage(@RequestBody WmNewsPageReqDto dto){
        return wmNewsService.findByPage(dto);
    }

    /**
     * 保存、修改、发布自媒体文章
     * @param wmNewsDto
     * @return
     */
    @PostMapping("/submit")
    public ResponseResult saveNews(@RequestBody WmNewsDto wmNewsDto){
        wmNewsService.saveNews(wmNewsDto);
        return ResponseResult.ok();
    }

    /**
     * 根据id查询自媒体文章
     */
    @GetMapping("/one/{id}")
    public ResponseResult<WmNewsResultDTO> findById(@PathVariable("id")Integer id){
        WmNews wmNews = wmNewsService.getById(id);
        if(wmNews==null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //数据转换
        WmNewsResultDTO wmNewsResultDTO = BeanHelper.copyProperties(wmNews, WmNewsResultDTO.class);
        //查询自媒体作者信息
        WmUser wmUser = wmUserService.getById(wmNews.getWmUserId());
        if (wmUser!=null) {
            wmNewsResultDTO.setAuthorName(wmUser.getName());
        }
        return ResponseResult.ok(wmNewsResultDTO);
    }

    /**
     * 根据id删除自媒体文章
     */
    @DeleteMapping("/del_news/{id}")
    public ResponseResult deleteById(@PathVariable("id")Integer id){
        wmNewsService.deleteNews(id);
        return ResponseResult.ok();
    }

    /**
     * 自媒体文章上下架功能
     */
    @PutMapping("/down_or_up")
    public ResponseResult deleteById(@RequestBody WmNewsDto wmNewsDto){
        wmNewsService.downOrUp(wmNewsDto);
        return ResponseResult.ok();
    }

    /**
     * 更新自媒体新闻状态
     * @param wmNewsDto
     * @return
     */
    @PutMapping("/update")
    public void updateWmNews(@RequestBody WmNewsDto wmNewsDto){
        //数据类型转换
        WmNews wmNews = BeanHelper.copyProperties(wmNewsDto, WmNews.class);
        wmNewsService.updateById(wmNews);
    }

    /**
     * 获取需要发布的文章id列表
     * @return
     */
    @GetMapping("/findPub")
    public List<Integer> findWmNewsIds() {
        return wmNewsService.findWmNewsIds();
    }

    /**
     * 条件查询自媒体文章列表
     */
    @PostMapping("/list")
    public PageResult<WmNewsResultDTO> findWmNewsPage(@RequestBody NewsAuthDto dto){
        return wmNewsService.findWmNewsPage(dto);
    }
}
